package com.darsn.fowtle.web.struts2;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.darsn.fowtle.core.domain.User;
import com.darsn.fowtle.core.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
public class LoginAction extends ActionSupport {
	
	private static final long serialVersionUID = -5671636500644613761L;
	
	private UserService userService;
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	private String username;
	private String password;
	private User user;
	
	public String getUsername() {
		return this.username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public User getUser() {
		return user;
	}

	@Action(value="login", 
			results={@Result(name="success", location="/index.jsp")})
	public String login() {
		user = userService.login(getUsername(), getPassword());
		
		if (user == null) {
			return LOGIN;
		}
		
		return SUCCESS;
	}
}
